package com.acme.entity;

import com.acme.entity.util.ChangeSupportList;
import com.acme.entity.util.EntityBean;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.swing.event.ChangeListener;

/**
 *
 * @author alejandro
 */
@Entity
@Table(name = "GASOLINERIA", catalog = "carreteras", schema = "dbo")
@NamedQueries({
    @NamedQuery(name = "Gasolineria.findAll", query = "SELECT g FROM Gasolineria g"),
    @NamedQuery(name = "Gasolineria.findById", query = "SELECT g FROM Gasolineria g WHERE g.id = :id"),
    @NamedQuery(name = "Gasolineria.findByKilometro", query = "SELECT g FROM Gasolineria g WHERE g.kilometro = :kilometro")})
public class Gasolineria extends EntityBean<Gasolineria> {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Column(name = "kilometro", length = 15)
    private String kilometro;
    @JoinTable(name = "GASOLINERIA_SERVICIO", joinColumns = {
        @JoinColumn(name = "gasolineria", referencedColumnName = "id", nullable = false)}, inverseJoinColumns = {
        @JoinColumn(name = "servicio", referencedColumnName = "id", nullable = false)})
    @ManyToMany
    private final ChangeSupportList<Servicio> servicioList = new ChangeSupportList<Servicio>();
    @ManyToMany(mappedBy = "gasolineriaList")
    private final ChangeSupportList<Combustible> combustibleList = new ChangeSupportList<Combustible>();
    @JoinColumn(name = "tramo", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private Tramo tramo;

    public Gasolineria() {
    }

    public Gasolineria(Integer id) {
        this.id = id;
    }

    public Gasolineria(Integer id, String kilometro, List<Servicio> servicioList, List<Combustible> combustibleList, Tramo tramo) {
        this.id = id;
        this.kilometro = kilometro;
        mergeListContent(this.servicioList, servicioList);
        mergeListContent(this.combustibleList, combustibleList);
        this.tramo = tramo;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
        support.fireChange();
    }

    public String getKilometro() {
        return kilometro;
    }

    public void setKilometro(String kilometro) {
        this.kilometro = kilometro;
        support.fireChange();
    }

    public List<Servicio> getServicioList() {
        return servicioList;
    }

    public void setServicioList(List<Servicio> servicioList) {
        mergeListContent(this.servicioList, servicioList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    public List<Combustible> getCombustibleList() {
        return combustibleList;
    }

    public void setCombustibleList(List<Combustible> combustibleList) {
        mergeListContent(this.combustibleList, combustibleList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    public Tramo getTramo() {
        return tramo;
    }

    public void setTramo(Tramo tramo) {
        this.tramo = tramo;
        support.fireChange();
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Gasolineria)) {
            return false;
        }
        Gasolineria other = (Gasolineria) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return String.format("GAS: km%s (%s)", kilometro, tramo);
    }

    @Override
    public void addChangeListener(ChangeListener listener) {
        combustibleList.addChangeListener(listener);
        servicioList.addChangeListener(listener);
        super.addChangeListener(listener);
    }

    @Override
    public Gasolineria clone() {
        return new Gasolineria(id, kilometro, servicioList, combustibleList, tramo);
    }

    @Override
    public void merge(Gasolineria other) {
        this.id = other.id;
        this.kilometro = other.kilometro;
        mergeListContent(this.servicioList, other.servicioList);
        mergeListContent(this.combustibleList, other.combustibleList);
        this.tramo = other.tramo;
    }

}
